package com.xiaoqi.api.controller;

import com.xiaoqi.api.utlis.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/file")
@Api(tags = "文件上传接口")
public class FileController {

    @PostMapping("/upload")
    public Result upload(String name, MultipartFile header){
        //header 可获取文件对应的属性

        System.out.println(name);
        //获取文件大小
        System.out.println("文件大小："+header.getSize());
        //获取文件名
        System.out.println("文件名："+header.getOriginalFilename());
        //获取文件名
        System.out.println(header.getName());
        //获取文件后缀名
        System.out.println(header.getContentType());

        //保存文件
        try {
            header.transferTo(new File("D://Upload//"+header.getOriginalFilename()));
            return Result.ok("成功!",200);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.ok("失败",500);
        }
    }
}
